﻿using FineUI;
using System;
using System.Linq;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.PhotoWall
{
    public partial class PhotoWallEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlCategory, "ZHDJ.PhotoWall.Category");
            ControlBusiness.ControlBindDataDictionary(ddlThematic, "ZHDJ.PhotoWall.Thematic", true);
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyPhotoWall photo = DB.PartyPhotoWalls.Find(id);
                if (photo == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                hdPhotoWallID.Text = id;
                txtTitle.Text = photo.Title;
                ddlCategory.SelectedValue = photo.Category;
                cbStatus.Checked = photo.Status == 4;
            }
            else
            {
                hdPhotoWallID.Text = StringHelper.NewID;
            }

            Grid1.DataSource = PartyPhotoWallImage.GetFileData(id);
            Grid1.DataBind();
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyPhotoWall photo = DB.PartyPhotoWalls.Find(id);
                if (photo == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                photo.Title = txtTitle.Text.Trim();
                photo.Category = ddlCategory.SelectedValue;
                photo.BranchID = this.BranchID;
                photo.Status = cbStatus.Checked ? 4 : 1;
                photo.Aduit = 2;
                photo.ModifyTime = DateTime.Now;
                photo.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyPhotoWall photo = new PartyPhotoWall();
                photo.ID = hdPhotoWallID.Text;
                photo.Title = txtTitle.Text.Trim();
                photo.Category = ddlCategory.SelectedValue;
                photo.BranchID = this.BranchID;
                photo.Status = cbStatus.Checked ? 4 : 1;
                photo.Aduit = 2;
                photo.CreateTime = DateTime.Now;
                photo.ModifyTime = DateTime.Now;
                photo.CreateBy = CurrentUser.LoginID;
                photo.ModifyBy = CurrentUser.LoginID;

                DB.PartyPhotoWalls.Add(photo);

                DB.SaveChanges();
            }
        }

        private void SaveImageData()
        {
            if (Grid1.Rows.Count > 0)
            {
                foreach (GridRow row in Grid1.Rows)
                {
                    int rowIndex = row.RowIndex;
                    System.Web.UI.WebControls.TextBox txtDesc = (System.Web.UI.WebControls.TextBox)Grid1.Rows[rowIndex].FindControl("txtDesc");

                    string rowDataId = Convert.ToString(Grid1.DataKeys[rowIndex][0]);

                    if (txtDesc != null)
                    {
                        PartyPhotoWallImage image = DB.PartyPhotoWallImages.Find(rowDataId);
                        image.Describe = txtDesc.Text.Trim();

                        DB.SaveChanges();
                    }
                }
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();
            SaveImageData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string wordId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                var del = DB.PartyPhotoWallImages.FirstOrDefault(t => t.ID == wordId);
                DB.PartyPhotoWallImages.Remove(del);
                DB.SaveChanges();

                Grid1.DataSource = PartyPhotoWallImage.GetFileData(hdPhotoWallID.Text);
                Grid1.DataBind();
            }
        }

        protected void fileImage_FileSelected(object sender, EventArgs e)
        {
            fileImage.Reset();
            UpFileResult upfileResult = UpFileBusiness.Upload(fileImage, CurrentUser.LoginID, "留影墙图片", SysEnum.UploadFileType.uploadimage); 

            //判断
            if (!upfileResult.IsSuccess)
            {
                Alert.Show(upfileResult.Message, MessageBoxIcon.Warning);
                return;
            }

            PartyPhotoWallImage photo = new PartyPhotoWallImage();
            photo.ID = StringHelper.NewID;
            photo.PhotoWallID = hdPhotoWallID.Text;
            photo.FileID = upfileResult.FileID;
            photo.Describe = "";
            photo.CreateTime = DateTime.Now;
            photo.ModifyTime = DateTime.Now;
            photo.CreateBy = CurrentUser.LoginID;
            photo.ModifyBy = CurrentUser.LoginID;

            DB.PartyPhotoWallImages.Add(photo);

            DB.SaveChanges();

            Grid1.DataSource = PartyPhotoWallImage.GetFileData(hdPhotoWallID.Text);
            Grid1.DataBind();
        }
        #endregion
    }
}